package com.xywl.escort.view.bandcard

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.adapter.BandCardDialogListAdapter
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.BandCardListResp
import com.xywl.escort.bean.BaseResp
import com.xywl.escort.common.PreferenceKeys.USER_ID
import com.xywl.escort.utils.RxSPTool
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_band_card_dialog_list.*
import timber.log.Timber
import java.util.*
import javax.inject.Inject

@Route(path = "/xy/band/card/dialog")
class BandCardDialogListActivity : BaseActivity(), BandCardListContract.View {

    private lateinit var mAdapter: BandCardDialogListAdapter
    private val mList = ArrayList<BandCardListResp.DataBean.ListBean>()

    private var pageNum = 1
    private var isEnd: Boolean = false

    @Autowired
    @JvmField
    var bandBean: BandCardListResp.DataBean.ListBean? = null

    @Inject
    lateinit var mPresenter: BandCardListContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_band_card_dialog_list)
        ARouter.getInstance().inject(this)
        initView()
        addListener()
    }

    override fun onResume() {
        super.onResume()
        pageNum = 1
        mPresenter.takeView(this)
        mPresenter.getBandCardList()
    }

    private fun initView() {
        val manager = LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.VERTICAL
        recycler_band_card.layoutManager = manager
        mAdapter = BandCardDialogListAdapter(R.layout.item_band_card_dialog, mList, bandBean)
        recycler_band_card.adapter = mAdapter
    }

    private fun addListener() {
        text_add_band_card.setOnClickListener {
            val userId = RxSPTool.getString(this, USER_ID)
            mPresenter.isSetPayPwd(userId)
        }

        mAdapter.setOnItemChildClickListener { _, _, position ->
            val listBean = mList[position]
            val intent = Intent()
            intent.putExtra("bandBean", listBean)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun getBandCardListSuccess(dataBean: BandCardListResp.DataBean?) {
        Timber.e("getBandCardListSuccess")
        recycler_band_card.visibility = View.VISIBLE

        if (pageNum == 1) {
            mList.clear()
        }
        if (pageNum >= dataBean!!.pages) {
            isEnd = true
        } else {
            isEnd = false
            pageNum += 1
        }
        mList.addAll(dataBean.list)
        mAdapter.setNewData(mList)
        mAdapter.loadMoreComplete()
    }

    override fun getBandCardListFailure() {
        Timber.e("getBandCardListFailure")
        recycler_band_card.visibility = View.VISIBLE
    }

    override fun hasSetPayPwd() {
        ARouter.getInstance().build("/xy/verify/pay/pwd")
                .withString("path", "/xy/band/card/bind")
                .navigation()
    }

    override fun noSetPayPwd() {
        ARouter.getInstance().build("/xy/pwd/setting/pay").navigation()
    }

    override fun findIsFitPayPwdFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }

    override fun updateUserBankCardDefStatusSuccess(resp: BaseResp?) {
    }

    override fun updateUserBankCardDefStatusFailure(msg: String?) {
    }
}
